<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="讲师姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="讲师头像" prop="avatar">
        <el-input v-model="form.avatar"></el-input>
      </el-form-item>
      <el-form-item label="讲师介绍" prop="introduce">
        <el-input type="textarea" :row="2" v-model="form.introduce"></el-input>
      </el-form-item>
      <el-form-item label="讲师年龄" prop="age">
        <el-input-number :min="1" v-model="form.age"></el-input-number>
      </el-form-item>
      <el-form-item label="讲师排序" prop="sort">
        <el-input-number :min="0" v-model="form.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="讲师性别" prop="gender">
        <el-radio v-model="form.gender" :label="0">女</el-radio>
        <el-radio v-model="form.gender" :label="1">男</el-radio>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacherApi from "@/api/teacher";
export default {
  name: "teacherSave",
  props: {
    teacherId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },
 
  data() {
    return {
      options: [],
      form: {
        id: "",
        name: "",
        avatar:'',
        introduce:'',
        age: 1,
        sort: 0,
        gender:0,
      },
      rules: {
        name: [{ required: true, message: "讲师姓名不能为空", trigger: "blur" }],
        avatar: [{ required: true, message: "讲师头像不能为空", trigger: "blur" }],
        introduce: [{ required: true, message: "讲师介绍不能为空", trigger: "blur" }],
      },
    };
  },
   watch: {
    close(newValue, oldValue) {
      this.form =  {
        id: "",
        name: "",
        avatar:'',
        introduce:'',
        age: 1,
        sort: 0,
        gender:0,
      };
    },
    teacherId(newValue, oldValue){
      if (this.teacherId) this.getInfo(this.teacherId);
    }
  },
  created() {
    if (this.teacherId) this.getInfo(this.teacherId);
  },
  methods: {
    // 获取数据
    async getInfo(id) {
      let res = await teacherApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.teacherId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await teacherApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await teacherApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>